#include "StdAfx.h"
#include "NormalBuffer.h"

CNormalBuffer* gNormalBuffer;

#define NORMALBUFFERSIZE	(100*1024)
CNormalBuffer::CNormalBuffer(void)
{
	m_hFile = NULL;
	m_width = NULL;
	m_height = NULL;

	m_buffersize = NORMALBUFFERSIZE;
	m_buffer = (char*)malloc(NORMALBUFFERSIZE);
	m_usersize = 0;
	RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::CNormalBuffer\r\n") ));
}

CNormalBuffer::~CNormalBuffer(void)
{
	if (m_hFile)
	{
		CloseHandle(m_hFile);
		m_hFile = NULL;
	}
	if (m_buffer)
	{
		free(m_buffer);
		m_buffer = NULL;
	}
}

bool CNormalBuffer::StartCapture(TCHAR* filename, DWORD width, DWORD height)
{
	if (m_hFile != NULL)
	{
		return FALSE;
	}
	m_hFile= CreateFile ( filename, GENERIC_WRITE, 0, NULL, 
		CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	m_width = width;
	m_height = height;
	m_usersize = 0;
	return TRUE;
}

bool CNormalBuffer::WriteFrame(byte* buffer, DWORD size)
{
	DWORD uiRet;
	RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::WriteFrame:1\r\n") ));
	if (m_hFile)
	{
		SingleJpegHead sjh;
		sjh.id = 0xAABBCCDD;
		GetLocalTime(&sjh.time);
		sjh.width = m_width;
		sjh.height = m_height;
		sjh.size =size;
		
		//RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::WriteFrame:2\r\n") ));

		if (NORMALBUFFERSIZE - m_usersize > sizeof(sjh))
		{
			//RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::WriteFrame:3\r\n") ));
			memcpy(m_buffer+m_usersize, &sjh, sizeof(sjh));
			m_usersize += sizeof(sjh);
		}else
		{
			//RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::WriteFrame:4\r\n") ));
			if (WriteFile(m_hFile, (void*)m_buffer,m_usersize, &uiRet, NULL) == 0)
			{
				RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::WriteFrame:WriteFile Error0\r\n") ));
			} else {
				//RETAILMSG(DEBUG, (_T("Write Head %ld =?= %ld\r\n"), uiRet, sizeof(sjh)));
			}
			m_usersize = 0;
			//RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::WriteFrame:5\r\n") ));
            memcpy(m_buffer+m_usersize, &sjh, sizeof(sjh));
            m_usersize += sizeof(sjh);
			//RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::WriteFrame:6\r\n") ));

		}

		if (NORMALBUFFERSIZE - m_usersize >  size)
		{
			//RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::WriteFrame:7\r\n") ));
			memcpy(m_buffer+m_usersize, buffer, size);
			m_usersize += size;
		} 
		else
		{
			//RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::WriteFrame:8\r\n") ));
			if (WriteFile(m_hFile, (void*)m_buffer,m_usersize, &uiRet, NULL) == 0)
			{
				RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::WriteFrame:WriteFile Error1\r\n") ));
			} else {
				//RETAILMSG(DEBUG, (_T("Write Head %ld =?= %ld\r\n"), uiRet, sizeof(sjh)));
			}
			m_usersize = 0;
            memcpy(m_buffer+m_usersize, buffer, size);
            m_usersize += size;
		}
		//RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::WriteFrame:9\r\n") ));
	} 
	else
	{
		RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::WriteFrame:not call StartCapture\r\n") ));
		return FALSE;
	}
	return TRUE;
}
void CNormalBuffer::StopCapture()
{
	DWORD uiRet;
	if (m_usersize >0)
	{
		if (WriteFile(m_hFile, (void*)m_buffer,m_usersize, &uiRet, NULL) == 0)
		{
			RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::StopCapture:StopCapture: save error!!!\r\n") ));
		} else {
			RETAILMSG(DEBUG, ( _T("jpegMov.lib: CNormalBuffer::StopCapture:StopCapture: Write Last\r\n") ));
		}
		m_usersize = 0;
	}
	CloseHandle(m_hFile);
	m_hFile = NULL;
}